package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.joda.time.DateTime;

import mappers.ContingentMapper;
import domains.Contingent;

public class ContingentHistoryGUI {
	private JFrame contingentHistoryFrame;
	private JTextArea historyArea;	
	private int contingentId, memberId;
	private DateTime startDate, endDate;
	private String startDateString, endDateString;
	private JScrollPane scrollPane;
	
	public ContingentHistoryGUI(final ShowMembersGUI gui) {
		historyArea = new JTextArea();
		historyArea.setFont(new Font("Arial", Font.BOLD, 16));
		historyArea.setLineWrap(true);
		historyArea.setWrapStyleWord(true);
		historyArea.setEditable(false);
		historyArea.setBackground(Color.WHITE);
		historyArea.setForeground(Color.black);
		
		scrollPane = new JScrollPane(historyArea);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		ContingentMapper cMapper = new ContingentMapper();
		ArrayList<Contingent> contingentInfo = cMapper
				.selectContingentInfo(gui.getIndex());
		
		memberId = contingentInfo.get(0).getMemberId();
		historyArea.append("MedlemsID: " + memberId + "\n\n");
		for (int i = 0; i < contingentInfo.size(); i++) {
			contingentId = contingentInfo.get(i).getContingentId();
			
			startDate = contingentInfo.get(i).getStartDate();
			endDate = contingentInfo.get(i).getEndDate();
			startDateString = startDate.getDayOfMonth() + "-"
					+ startDate.getMonthOfYear() + "-"
					+ startDate.getYear();
			endDateString = endDate.getDayOfMonth() + "-"
					+ endDate.getMonthOfYear() + "-"
					+ endDate.getYear();
			
			historyArea.append("KontingentID: " + contingentId + "\nPeriode: " + 
					startDateString + " - " + endDateString + "\n\n");

		}
	
		//historyArea.append("KontingentID: " + contingentId + "\nPeriode: " + 
				//startDateString + " - " + endDateString);
		
	}
	
	public void showGUI() {
		contingentHistoryFrame = new JFrame();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		int w = contingentHistoryFrame.getSize().width;
		int h = contingentHistoryFrame.getSize().height;
		int x = (dim.width-w)/4;
		int y = (dim.height-h)/4;
		contingentHistoryFrame.setLocation(x, y);
		contingentHistoryFrame.setTitle("Kontingenthistorie for medlemsID " + memberId);
		contingentHistoryFrame.setLayout(new BorderLayout());
		contingentHistoryFrame.setSize(400, 300);
		contingentHistoryFrame.add(scrollPane);
		// showMembersFrame.pack();
		contingentHistoryFrame.setVisible(true);
	}
}
